package whf.framework.file.wraper;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import whf.framework.datasource.DataRow;
import whf.framework.exception.PropertyException;
import whf.framework.file.IFile;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.util.Base64;
import whf.framework.util.BeanUtils;
import whf.framework.util.Pair;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.web.tag.TableColumn;
import whf.framework.web.tag.TableRow;
import whf.framework.web.tag.wrapper.IDisplayWrapper;

/**
 * @author wanghaifeng
 * @create Dec 29, 2006 5:31:14 PM
 * 
 */
public class FileDisplayWrapper implements IDisplayWrapper {
	private static Log log = LogFactory.getLog(FileDisplayWrapper.class);
	
	protected final static List<Pair<String, String>> defaultProperties = Utils.newArrayList();
	
	static {
		defaultProperties.add(new Pair<String, String>("名称", "name"));
	}
	
	protected String scale;
	
	protected boolean specific;
	
	protected List<Pair<String, String>> properties;
	
	public FileDisplayWrapper(String scale, boolean specific, List<Pair<String, String>> properties){
		this.scale = scale;
		this.specific = specific;
		this.properties = properties;
	}
	
	/**
	 * 返回图片URI和sourceURI
	 * @modify wanghaifeng Mar 31, 2007 3:34:01 PM
	 * @param pageContext
	 * @param file
	 * @return
	 */
	public Pair<String, String> getFileURI(PageContext pageContext, IFile file){
		HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
		String sourceFile = file.getPhycialPath();
		if(sourceFile == null) sourceFile = "";
		
		String path = sourceFile;
		int li = path.lastIndexOf(".");
		String ext = "";
		if(path.length() > 0) {
			ext = path.substring(li + 1);
			if(!StringUtils.isEmpty(this.scale)) {
				path = path.substring(0, li) + "." + this.scale + "."+ext;
			}
		}
		String sourceUrl = req.getContextPath() + "/Download?path="+Base64.encode(sourceFile);
		String url = req.getContextPath() + "/Download?path="+Base64.encode(path);
		return new Pair<String, String>(sourceUrl, url);
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.web.tag.wrapper.IDisplayWrapper#writeBlockHtml(java.io.Writer, whf.framework.web.tag.TableRow)
	 */
	public void writeHtml(PageContext pageContext, List<TableColumn> displayColumns, TableRow row) throws JspException, IOException {
		Writer writer = pageContext.getOut();
		IFile file = (IFile)row.getRowObject();
		
		Pair<String, String> urls = this.getFileURI(pageContext, file);
		String sourceUrl = urls.getText();
		String url = urls.getValue();

		writer.write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>\n");
		writer.write("<tr><td colspan=2>");
		if(this.specific){
			writer.write("<a name='pic' href='"+sourceUrl+"' rel='lightbox[plants]'>");
		}
		writer.write("<img src='"+url+"' border='0'>");
		if(this.specific){
			writer.write("</a>");
		}
		writer.write("</td></tr>");
		try{
			if(this.properties != null && !this.properties.isEmpty()){
				DataRow bo = row.getRowObject();
				for(Pair<String, String> pair: this.properties){
					String title = pair.getText();
					String name = pair.getValue();
					String value = "";
					try{
						Object propertyValue = BeanUtils.getProperty(bo, name);
						if(propertyValue != null){
							value = propertyValue.toString();
						}
					} catch(PropertyException e){
						log.error(e);
					}
					if(title == null) title = "";
					writer.write("<tr><td width='30%'>" + title + "</td><td width='*%'>" + value +"</td></tr>\n");
				}
			}
		}catch(Exception e){
			log.error(e);
		}
		writer.write("</table>\n");
	}

}
